/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.blitscreen.gesture.strategy
{
	import com.blitscreen.render.BlitSprite;
	
	/**
	 * The <code>PixelHit</code> strategy detects collision by comparison
	 * of the mouse position with the pixel of the sprite.
	 * <p>
	 * In order to optimize the detection, a first test is performed
	 * using the bouding box approach, and if the mouse cursor is inside
	 * the bounding box, a pixel comparison is performed.
	 * </p><p>
	 * Pixel comparison is realized on the alpha channel, if the value
	 * of the alpha channel is greater than the <code>threshold</code>
	 * property, the collision occurs.  
	 * </p>
	 */
	public class PixelHit extends BoundingBoxHit implements GestureHitMode
	{
		/**
		 * Alpha value upon which detection occurs.
		 */
		public var threshold : Number = 0;
		
		/**
		 * @inheritDoc
		 */
		override public function isTouched( mouseX : Number, mouseY : Number, sprite : BlitSprite ) : Boolean
		{
			if( super.isTouched( mouseX, mouseY, sprite ) )
			{
				var x : Number = mouseX - ( sprite.position.x - sprite.center.x ) + sprite.area.x;
				var y : Number = mouseY - ( sprite.position.y - sprite.center.y ) + sprite.area.y;
				
				return ( ( sprite.bitmap.getPixel32( x, y ) >> 24 & 0xff ) > threshold );
			}
			else return false;
		}
	}
}